package com.sfang.common.redis.config;

import com.sfang.common.redis.sdk.RedisClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.annotation.PostConstruct;

@Configuration
public class RedisRedissonConfig {
    @Autowired
    Environment environment;

    @Value("${master.redis.key.prefix:}")
    private String redisKeyPrefix;

    @Value("${master.redis.nodes:}")
    private String redisNodes;

    @Value("${master.redis.database:}")
    private Integer redisDatabase;

    private static Logger LOGGER = LoggerFactory.getLogger(RedisRedissonConfig.class);

    @PostConstruct
    public void init() {

    }

    @Bean(destroyMethod = "shutdown")
    public RedisClient redisClient() {
        LOGGER.error(String.format("RedisClusterConfig redisNodes:%s", redisNodes));

        if (redisNodes == null || redisNodes == "")
            return null;

        RedisClusterConfiguration configuration = new RedisClusterConfiguration(redisNodes);
        RedisClient redisClient = new RedissonClient(configuration, redisDatabase,redisKeyPrefix);
        return redisClient;
    }
}
